package com.yungu.swift.admin.route.operation;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.admin.base.CurrentUser;
import com.yungu.swift.admin.consts.AdminErrorEnum;
import com.yungu.swift.assets.passenger.model.dto.PassengerBlockedLogDto;
import com.yungu.swift.assets.passenger.model.dto.PassengerCouponDto;
import com.yungu.swift.assets.passenger.model.dto.PassengerDto;
import com.yungu.swift.assets.passenger.model.dto.PassengerEmergencyMemberDto;
import com.yungu.swift.assets.passenger.model.param.AdminPassengerAbortParam;
import com.yungu.swift.assets.passenger.model.param.AdminPassengerPageParam;
import com.yungu.swift.assets.passenger.model.param.AdminPassengerUuidPageParam;
import com.yungu.swift.assets.passenger.service.PassengerCouponService;
import com.yungu.swift.assets.passenger.service.PassengerEmergencyMemberService;
import com.yungu.swift.assets.passenger.service.PassengerPunishLogService;
import com.yungu.swift.assets.passenger.service.PassengerService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.base.model.admin.AdminParam;
import com.yungu.swift.base.model.annotation.ResponseImgOverride;
import com.yungu.swift.constants.MagicConstant;
import com.yungu.swift.constants.PassengerConstant;
import com.yungu.swift.system.marketing.model.param.AdminGiftUuidPageParam;
import com.yungu.swift.utils.DateUtils;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.ParamUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @author : cuixiuyin
 * @date : 2019/6/21
 */
@RestController
@RequestMapping("/admin/v1/operation/passenger")
@Api(value = "【运营管理】", tags = "乘客信息管理")
public class PassengerAdmin {

    @Reference
    private PassengerService passengerService;
    @Reference
    private PassengerPunishLogService passengerPunishLogService;
    @Reference
    private PassengerEmergencyMemberService passengerEmergencyMemberService;
    @Reference
    private PassengerCouponService passengerCouponService;

    @ResponseImgOverride
    @RequestMapping(value = "/queryPage", method = RequestMethod.POST)
    @ApiOperation(value = "乘客信息分页查询", httpMethod = "POST")
    public ResponseData<PageVo<PassengerDto>> queryPage(@RequestBody AdminPassengerPageParam passengerPageAdmin) {
        return passengerService.queryPage(passengerPageAdmin);
    }

    @ResponseImgOverride
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ApiOperation(value = "乘客信息详情", httpMethod = "GET")
    public ResponseData<PassengerDto> detail(@RequestParam String uuid, @CurrentUser AdminParam adminParam) {
        if (ParamUtil.isIllegal(uuid)) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        Map<String, Object> paramMap = MapUtils.build("uuid", uuid);
        paramMap.put("userRoleUuid", adminParam.getUserRoleUuid());
        List<PassengerDto> passengerList = passengerService.listExtend(paramMap).getData();
        if (CollectionUtils.isNotEmpty(passengerList)) {
            return ResponseData.buildSuccessResponse(passengerList.get(0));
        }
        return ResponseData.buildErrorResponse(AdminErrorEnum.RESULT_EMPTY.getCode(), AdminErrorEnum.RESULT_EMPTY.getMsg());
    }

    @RequestMapping(value = "/abort", method = RequestMethod.POST)
    @ApiOperation(value = "乘客封号/解封操作", httpMethod = "POST")
    public ResponseData<Boolean> abort(@RequestBody AdminPassengerAbortParam passengerAbortParam) {
        if (ParamUtil.isIllegal(passengerAbortParam.getUuid(), passengerAbortParam.getStatus())) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        return passengerService.abort(passengerAbortParam);
    }

    @ResponseImgOverride
    @RequestMapping(value = "/queryDrawPage", method = RequestMethod.POST)
    @ApiOperation(value = "获取乘客礼包领取记录", httpMethod = "POST")
    public ResponseData<PageVo<PassengerDto>> queryDrawPage(@RequestBody AdminGiftUuidPageParam giftUuidPageParam) {
        if (ParamUtil.isIllegal(giftUuidPageParam.getGiftUuid())) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        return passengerService.queryDrawPage(giftUuidPageParam);
    }

    @RequestMapping(value = "/punishLogPage", method = RequestMethod.POST)
    @ApiOperation(value = "获取乘客的封号记录列表", httpMethod = "POST")
    public ResponseData<PageVo<PassengerBlockedLogDto>> punishLogPage(@RequestBody AdminPassengerUuidPageParam passengerUuidPageParam) {
        if (ParamUtil.isIllegal(passengerUuidPageParam.getPassengerUuid())) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        return passengerPunishLogService.queryPage(passengerUuidPageParam);
    }

    @RequestMapping(value = "/emergencyMemberPage", method = RequestMethod.POST)
    @ApiOperation(value = "获取乘客的紧急联系成员列表", httpMethod = "POST")
    public ResponseData<PageVo<PassengerEmergencyMemberDto>> emergencyMemberPage(@RequestBody AdminPassengerUuidPageParam passengerUuidPageParam) {
        if (ParamUtil.isIllegal(passengerUuidPageParam.getPassengerUuid())) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        return passengerEmergencyMemberService.queryPage(passengerUuidPageParam);
    }


    @RequestMapping(value = "/export", method = RequestMethod.POST)
    @ApiOperation(value = "乘客信息导出", httpMethod = "POST")
    public void exportData(@RequestBody AdminPassengerPageParam passengerPageParam, HttpServletResponse response) throws Exception {
        try (OutputStream os = response.getOutputStream()) {
            List<PassengerDto> beans = passengerService.listExport(passengerPageParam).getData();
            Workbook workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet();
            //宽度5.38
            sheet.setColumnWidth(0, ((5 + 1) * 7 + 5) / 7 * 256);
            //宽度50.38
            sheet.setColumnWidth(1, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(2, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(3, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(4, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(5, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(6, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(7, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(8, ((25 + 1) * 7 + 5) / 7 * 256);
            //创建第一行
            Row row = sheet.createRow(0);
            row.createCell(0).setCellValue("序号");
            row.createCell(1).setCellValue("联系方式");
            row.createCell(2).setCellValue("姓名");
            row.createCell(4).setCellValue("订单总量");
            row.createCell(3).setCellValue("性别");
            row.createCell(5).setCellValue("未支付订单量");
            row.createCell(6).setCellValue("投诉订单量");
            row.createCell(7).setCellValue("注册时间");
            row.createCell(8).setCellValue("状态");

            for (int rowNum = 1; rowNum <= beans.size(); rowNum++) {
                Row dataRow = sheet.createRow(rowNum);
                dataRow.createCell(0).setCellValue(rowNum);
                PassengerDto bean = beans.get(rowNum - 1);
                for (int cellNum = 1; cellNum <= MagicConstant.INT_NINE; cellNum++) {
                    Cell cell = dataRow.createCell(cellNum);
                    switch (cellNum) {
                        case 1: {
                            if (bean.getMobile() != null) {
                                cell.setCellValue(bean.getMobile());
                            }
                            break;
                        }
                        case 2: {
                            if (bean.getNickname() != null) {
                                cell.setCellValue(bean.getNickname());
                            }
                            break;
                        }
                        case 3: {
                            if (bean.getSex() != null) {
                                if (bean.getSex().equals(PassengerConstant.SEX_MAN)) {
                                    cell.setCellValue("男");
                                } else if (bean.getSex().equals(PassengerConstant.SEX_WOMAN)) {
                                    cell.setCellValue("女");
                                } else {
                                    cell.setCellValue("");
                                }
                            }
                            break;
                        }
                        case 4: {
                            cell.setCellValue(bean.getOrderCount());
                            break;
                        }
                        case 5: {
                            cell.setCellValue(bean.getNoPayCount());
                            break;
                        }
                        case 6: {
                            cell.setCellValue(bean.getComplantCount());
                            break;
                        }
                        case 7: {
                            cell.setCellValue(DateUtils.format(bean.getCreateOn(), "yyyy-MM-dd HH:mm:ss"));
                            break;
                        }
                        case 8: {
                            if (bean.getStatus() != null) {
                                if (bean.getStatus().equals(PassengerConstant.PASSENGER_STATUS_NORMAL)) {
                                    if (bean.getActive().equals(PassengerConstant.PASSENGER_STATUS_NORMAL)) {
                                        cell.setCellValue("正常");
                                    } else {
                                        cell.setCellValue("未激活");
                                    }
                                } else if (bean.getStatus().equals(PassengerConstant.PASSENGER_STATUS_LONG_CLOSE)) {
                                    cell.setCellValue("长期封号");
                                } else if (bean.getStatus().equals(PassengerConstant.PASSENGER_STATUS_SHORT_CLOSE)) {
                                    cell.setCellValue("短期封号");
                                } else {
                                    cell.setCellValue("");
                                }
                            }
                            break;
                        }
                        default:
                            break;
                    }
                }
            }

            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("passenger.xlsx", "UTF-8"));
            response.setContentType("application/octet-stream");
            response.setCharacterEncoding("utf-8");
            workbook.write(os);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取乘客的优惠券的列表
     */
    @RequestMapping("/couponList")
    @ResponseBody
    public ResponseData<PageVo<PassengerCouponDto>> adminCouponList(@RequestBody AdminPassengerUuidPageParam param) throws Exception {
        return passengerCouponService.adminCouponList(param);
    }
}
